<?php
/**
 * Created by PhpStorm.
 * User: maqun
 * Date: 2015/11/24
 * Time: 21:44
 *  前台页面显示
 */
class IndexAction extends BaseAction {

    protected $static_stime ;
    protected $static_etime ;

    public function _initialize(){
        self::$action = 0;  //
        parent::_initialize();

        $this->static_stime = date('Y-m-d H:i:s',time()-100*24*3600);
        $this->static_etime = date('Y-m-d H:i:s',time());
        $this->assign('start_time',$this->static_stime);
        $this->assign('end_time',$this->static_etime);
    }

    public function index(){
        $this->assign('sub_str',$res['sub']);
        $this->assign('ac_str',$res['ac']);
        //新闻
        $news = D('News')
            ->where("defunct!='Y'")
            ->order("importance ASC,time DESC ")
            ->limit(5)
            ->select();

        //获取本周排名TOP10
        $top_rank = D('Solution')->getTopRank($this->static_stime,$this->static_etime);

        $this->assign('top_rank',$top_rank);
        $this->assign('news',$news);
        $this->display('index');
    }
    /**
     * 获取统计数据,每天提交/AC数量
     */
    public function getStatistics(){

//        $start = $_POST['s_date'];
//        $end   = $_POST['e_date'];
        $start = !empty($_POST['s_date']) ? date('Y-m-d H:i:s',strtotime($_POST['s_date'])):date('Y-m-d H:i:s',time()-100*24*60*60);
        $end   = !empty($_POST['e_date']) ? date('Y-m-d H:i:s',strtotime($_POST['s_date'])):date('Y-m-d H:i:s',time());

        //统计数据
        $res = D('Solution')->getInfo($start,$end);
//        echo D('Solution')->_sql();
        if($res['status']){
            $this->ajaxReturn($res);
        }else{
            $this->ajaxReturn(array('status'=>false,'info'=>'暂无数据'));
        }
        exit();
    }
    /**
     * 获取统计数据,按年级提交/AC数量
     */
    public function getGradeRank(){
        $start = date('Y-m-d H:i:s',time()-100*24*60*60);
        $end   = date('Y-m-d H:i:s',time());

        $info = D('Solution')->getGradeRank($start,$end);
        if(!empty($info)){
            $return['status'] = true;
            $return['info']['cate_sub'] = $info['grade'];
            for($i=0;$i<count($info['ac']);$i++){
                $return['info']['ac'][] = intval($info['ac'][$i]['total']);
                $return['info']['sub'][] = intval($info['sub'][$i]['total']);
            }
        }else{
            $return['status'] = false;
            $return['info']   = "暂无数据";
        }
        $this->ajaxReturn($return);
    }
}